package com.ruoyi.yljf.controller.AppController;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.yljf.domain.Drug;
import com.ruoyi.yljf.domain.dto.CommonTypeDTO;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.DrugVO;
import com.ruoyi.yljf.service.ICollectRecordService;
import com.ruoyi.yljf.service.IDrugService;
import com.ruoyi.yljf.util.CommonUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 药材
 */
@Anonymous
@RestController
@RequestMapping("/api/drug")
public class ApiDrugController extends BaseController {

    @Resource
    private IDrugService drugService;
    @Resource
    private ICollectRecordService collectRecordService;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 查询药材列表
     */
    @GetMapping("/list")
    public AjaxResult getDrugList(CommonTypeDTO dto) {
        return success(drugService.getDrugVO(dto));
    }

    /**
     * 查询药材列表
     */
    @GetMapping("/listV2")
    public AjaxResult getDrugV2VO(CommonTypeDTO dto) {
        return success(drugService.getDrugV2VO(dto));
    }

    /**
     * 根据id查询药材详情
     */
    @GetMapping("/getDrugById")
    public AjaxResult getDrugById(@RequestParam Long id, HttpServletRequest req) {
        Drug byId = drugService.getById(id);
        DrugVO drugVO = BeanUtil.copyProperties(byId, DrugVO.class);
        if(drugVO!=null && commonUtils.userIfLogin(req))
            drugVO.setCollect(collectRecordService.ifCollect(yljfEnums.NOTICE_TYPE_DRUG.getCode(),id,getUserId()));
        return success(drugVO);
    }

}
